<?php
/** .-------------------------------------------------------------------
* |  Software: [HDCMS framework]
* |      Site: www.hdcms.com
* |-------------------------------------------------------------------
* |    Author: 向军 <2300071698@qq.com>
* |    WeChat: aihoudun
* | Copyright (c) 2012-2019, www.houdunwang.com. All Rights Reserved.
* '-------------------------------------------------------------------*/

namespace app\admin\controller;


class Goods extends Common {
    private  $db;
    public function __construct()
    {
        parent::__construct();
        $this->db= new \system\model\Goods();
    }

    //动作
    public function index(){
    //此处书写代码...
        $goodsData=Db::table('goods')->paginate(5);
        $page=$goodsData->links();
        View:: with("goodsData",$goodsData)->with("page",$page);
        return view();

    }
    public function add(){
          if(IS_POST){
              if($this->db->store()){
                 message("添加成功","","success");
              }
              message($this->db->getError(),"","error");
          }
        //
        $cateData=Data::tree(Db::table("category")->get(),'cname');
        View::with("cateData",$cateData);
        $brandData=Db::table("brand")->get();
        View::with("brandData",$brandData);
        return view();
    }
    //获取类型属性的所有数据
    public function getAttr(){
        $tid = Q('post.tid');
        $typeAttr = Db::table('type_attr')->where('tid',$tid)->get();
        foreach($typeAttr as $k=>$v){
            $tavalue=str_replace('，',',',$v['tavalue']);
            $typeAttr[$k]['tavalue'] = explode(',',$tavalue);
        }
        echo json_encode($typeAttr);exit;
    }


    //删除
    public function del(){
        $gid=Q('g');
        //删除商品表
        $this->db->where('gid',$gid)->delete();
        //删除商品属性
        $goodsAttr=new \system\model\GoodsAttr();
        $goodsAttr->where("gid",$gid)->delete();
        //删除商品详情表
        $Detail=new \system\model\Detail();
        $Detail->where("gid",$gid)->delete();
        message('删除成功','','success');
    }


   //编辑
    public function edit(){
        if(IS_POST){
            if($this->db->edit()){
               message('编辑成功','','success');
            }
            message($this->db->getError(),'','error');
        }
        //获取商品表和商品详情表的旧数据
        $gid=Q('get.gid');
        $oldData = Db::table('goods')->join("detail","goods.gid","=","detail.gid")->where('goods.gid',$gid)->first();
        //将字符串转化为数组并将汉字“逗号”转化成英文的“逗号”
        $medium=str_replace('，',',',$oldData['medium']);
        $oldData['medium']=explode(',',$medium);
        View::with('oldData',$oldData);
        //获取分类表的旧数据，以name值为条件进行树状
        $cateData=Data::tree(Db::table('category')->get(),'cname');
        View::with('cateData',$cateData);
        //获取品牌表的旧数据
        $brandData=Db::table("brand")->get();
        View::with("brandData",$brandData);
         //获取商品属性
         $attrData=Db::table('type_attr')
                    ->where('tid',$oldData['tid'])
                    ->where('class',2)
                    ->get();
        //循环数据将类型属性表的属性数据中的name值
        //为tavalue的字符串转化成数组
        foreach ($attrData as $k=>$v){
            $tavalue=str_replace('，',',',$v['tavalue']);
            $attrData[$k]['tavalue']=explode(',',$tavalue);
        }
        //获取选中的商品属性
        $goodsAttrData = Db::table('goods_attr')
            ->where('gid',$gid)
            ->lists('gtvalue');
         View::with('attrData',$attrData)->with('goodsAttrData',$goodsAttrData);
        //获取商品规格
        $specData=Db::table('goods_attr')
                  ->join('type_attr','goods_attr.taid','=','type_attr.taid')
                  ->where('class',1)
                  ->where('gid',$gid)
                  ->get();
        foreach ($specData as $k=>$v){
            $tavalueData=str_replace('，',',',$v['tavalue']);
            $specData[$k]['tavalue']=explode(',',$tavalueData);
        }
        View::with('specData',$specData);
        return view();
    }
}
